//
//  FQBankCardViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2023/4/5.
//

import UIKit

class FQBankCardViewController: BaseViewController, UITableViewDataSource, UITableViewDelegate {
    let tableView = UITableView(frame: .zero, style: .plain)
    
    var isEmpty = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.rowHeight = 140
        tableView.register(FQEmptyBankCardTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.register(FQBankCardTableViewCell.self, forCellReuseIdentifier: "cardCell")
        tableView.backgroundColor = .colorMake(244, 246, 250)
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(NavigationContentTop)
        }
        if !isEmpty {
            tableView.tableFooterView = footerView()
        }
    }
    
    func footerView() -> UIView {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 80))
        let button = QMUIButton(title: "添加银行卡", titleColor: .colorMain, font: .rSFont(16))
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 6
        button.setImage(UIImage(named: "bankCard_add2"), for: .normal)
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 23, bottom: 0, right: 23)
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        button.layer.borderColor = UIColor.colorMain.cgColor
        button.layer.borderWidth = 1
        button.layer.cornerRadius = 20
        button.clipsToBounds = true
        footerView.addSubview(button)
        
        button.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(40)
        }
        return footerView
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isEmpty {
            return 1
        } else {
            return 10
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if isEmpty {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FQEmptyBankCardTableViewCell
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "cardCell") as! FQBankCardTableViewCell
        cell.model = ""
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if tableView.cellForRow(at: indexPath)!.isKind(of: FQEmptyBankCardTableViewCell.self) {
            buttonClick()
            return
        }
        
    }
    
    @objc func buttonClick() {
        navigationController?.qmui_pushViewController(FQAddBankCardViewController(), animated: true)
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "银行卡"
    }
}
